/**
 * @param time datetime
 * @param cFormat
 * @returns {*} yyyy-mm-dd hh:min:ss
 */
export function parseTime(time, cFormat) {
    if (arguments.length === 0) {
        return null;
    }
    const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
    let date;
    if (typeof time === "object") {
        date = time;
    } else {
        if (typeof time === "string" && /^[0-9]+$/.test(time)) {
        time = parseInt(time);
        }
        if (typeof time === "number" && time.toString().length === 10) {
        time = time * 1000;
        }
        date = new Date(time);
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
    };
    const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key];
        if (key === "a") {
        return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
        }
        // if (result.length > 0 && value < 10) {
        //   value = '0' + value
        // }
        return value || 0;
    });
    return timeStr;
}

/**
 * 秒数转为 hh:ss
 * @param {*} duration 秒数
 */
export function duration2mmss(duration) {
    let sec = parseInt(duration);
    let minutes = Math.floor(sec / 60);
    let seconds = sec - minutes * 60;

    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    return minutes + ":" + seconds;
}

/**
 * 产生随机整数，包含下限值，但不包括上限值
 * @param {Number} lower 下限
 * @param {Number} upper 上限
 * @return {Number} 返回在下限到上限之间的一个随机整数
 */
export function randomInt(lower, upper) {
  return Math.floor(Math.random() * (upper - lower)) + lower;
}

/**
 * 数据类型判断
 */
function typeOf(obj) {
    const toString = Object.prototype.toString;
    const map = {
        "[object Boolean]": "boolean",
        "[object Number]": "number",
        "[object String]": "string",
        "[object Function]": "function",
        "[object Array]": "array",
        "[object Date]": "date",
        "[object RegExp]": "regExp",
        "[object Undefined]": "undefined",
        "[object Null]": "null",
        "[object Object]": "object",
    };
    return map[toString.call(obj)];
}

/**
 * 拷贝
 */
export function deepCopy(data) {
    const t = typeOf(data);
    let o;

    if (t === "array") {
        o = [];
        for (let i = 0; i < data.length; i++) {
        o.push(deepCopy(data[i]));
        }
    } else if (t === "object") {
        o = {};
        for (let i in data) {
        o[i] = deepCopy(data[i]);
        }
    } else {
        return data;
    }

    return o;
}

/**
 * e 表示长度，默认32位
 */
export function randomString(e) {
    e = e || 32;
    let t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
    let a = t.length;
    let n = "";
    for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n;
}

/**
 * 对比两个数组是否相等
 */
export function equar(a, b) {
    // 判断数组的长度
    if (a.length !== b.length) {
        return false;
    } else {
        // 循环遍历数组的值进行比较
        for (let i = 0; i < a.length; i++) {
        if (a[i] !== b[i]) {
            return false;
        }
        }
        return true;
    }
}

/**
 * 获取图片信息
 * @param url 图片地址
 */
export function urlToImgObj(url) {
    return new Promise((resolve, reject) => {
        // 创建image元素
        const img = new Image();
        img.src = url;
        // 在onload中才可以得到img的width和height
        img.onload = () => {
        const res = {
            dom: img,
            url: url,
            width: img.width,
            height: img.height,
        };
        resolve(res);
        };
    });
}

/**
 * 邮箱验证
 * @param val
 * @returns {*|boolean}
 */
export function emailValidator(val = "") {
    const reg =
        /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}/;
    return reg.test(val);
}

/**
 * 手机号验证
 * @param val
 * @returns {*|boolean}
 */
export function phoneValidator(val = "") {
    const reg =
        /^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/;
    return reg.test(val);
}
